﻿using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
using XZTZ.UMP.Public.Common;
using XZTZ.UMP.Public.Models.Change;
using XZTZ.UMP.WEB.Data;
namespace XZTZ.UMP.WEB.Pages.Change.ReportVision1
{
    public class ExcelDesignSumModel : PageModel
    {
        private readonly ApplicationDbContext _context;

        public ExcelDesignSumModel(ApplicationDbContext context)
        {
            _context = context;
        }

        public DataTable tabResut { get; set; }
        public v_ChangeBaseInfo v_ChangeBaseInfo { get; set; }
        public decimal PreMonthChange { get; set; }
        public decimal CurrentMoneyChange { get; set; }
        public decimal SumChange { get; set; }
        public decimal PreMonthChange1 { get; set; }
        public decimal CurrentMoneyChange1 { get; set; }
        public decimal SumChange1 { get; set; }
        public async Task OnGet()
        {
            var tempGuid = new Guid("28972e13-1a71-48cb-96ac-596c65a4c914");
            v_ChangeBaseInfo = await _context.v_ChangeBaseInfo.SingleAsync(x => x.guidChangeID == tempGuid);
            var aa = new SqlParameter("@guidChangeID", tempGuid);
            SqlParameter[] sqlParams = new SqlParameter[]{ aa };
            DataSet dsResult = ExtendDBContext.ExecProcReader(_context, "Change_GetProjectSumByChangeID", sqlParams);
            tabResut = dsResult.Tables[0];
            AddIndexNo(tabResut.DefaultView);
            InitHeaderContent(dsResult.Tables[1].Rows[0]);
        }
        #region 添加序号
        private void AddIndexNo(DataView vewData)
        {
            int i = 0, j = 0;
            foreach (DataRowView _Row in vewData)
            {
                if ((string)_Row["strComputeType"] == "01")
                {
                    i++;
                    j = 0;
                    _Row["ItemNo"] = PublicMethod.Arabia_to_Chinese(i);
                }
                else
                {
                    j++;
                    _Row["ItemNo"] = j.ToString();
                }
            }
        }
        #endregion

        #region 得到汇总变量
        private void InitHeaderContent(DataRow _Row)
        {
            PreMonthChange = (decimal)_Row["PreMonthChange"];
            CurrentMoneyChange = (decimal)_Row["CurrentMoneyChange"];
            SumChange = (decimal)_Row["SumChange"];

            if (PreMonthChange < 0)//上月末累计变更
            {
                PreMonthChange1 = Math.Abs(PreMonthChange);
                PreMonthChange = 0;
            }
            if (CurrentMoneyChange < 0)//本月累计变更
            {

                CurrentMoneyChange1 = Math.Abs(CurrentMoneyChange);
                CurrentMoneyChange = 0;
            }
            if (SumChange < 0)          //累计变更
            {
                SumChange1 = Math.Abs(SumChange);
                SumChange = 0;
            }
        }
        #endregion
    }
}